// 导入threejs
import * as THREE from "three";

// 创建一个随机的点集合体
class RandomSpacePoints extends THREE.Points {
  constructor(num = 100) {
    super();
    this.name = "RandomSpacePoints";
    // 创建集合体对象
    const geometry = new THREE.BufferGeometry();
    // 创建顶点数据
    const vertices = new Float32Array(num * 3); // 每个顶点有x、y、z三个坐标
    // 创建颜色数据
    const colors = new Float32Array(num * 3);
    // 创建大小数据
    const sizes = new Float32Array(num);
    // 创建随机方向数据
    const randomDirections = new Float32Array(num * 3);
    // 创建随机速度数据
    const randomSpeeds = new Float32Array(num);
    // 创建范围
    let radius = 5;
    for (let i = 0; i < num; i++) {
      // 随机生成x、y、z坐标，范围在0到5之间
      vertices[i * 3] = radius * (Math.random() - 0.5); // x
      vertices[i * 3 + 1] = 0.5 * radius * (Math.random() - 0.5); // y
      vertices[i * 3 + 2] = 0.5 * radius * (Math.random() - 0.5); // z

      // 随机生成颜色
      colors[i * 3] = Math.random() * 0.5;
      colors[i * 3 + 1] = Math.random() * 0.5;
      colors[i * 3 + 2] = Math.random() * 0.5 + 0.5;

      // 随机生成大小
      sizes[i] = Math.random() * 0.5 + 0.5;

      // 随机生成速度
      randomSpeeds[i] = Math.random() * 0.5 + 0.1;
      // 随机生成方向
      let randomDirection = new THREE.Vector3(
        Math.random() * 4 - 2, // X 方向 -1 到 1 之间的随机数
        Math.random() * 4 - 2, // Y 方向 -1 到 1 之间的随机数
        Math.random() * 4 - 2 // Z 方向 -1 到 1 之间的随机数
      ).normalize();
      randomDirections[i * 3] = randomDirection.x; // 归一化以确保方向一致
      randomDirections[i * 3 + 1] = randomDirection.y;
      randomDirections[i * 3 + 2] = randomDirection.z;
    }

    // 设置顶点到几何体
    geometry.setAttribute("position", new THREE.BufferAttribute(vertices, 3));
    geometry.setAttribute("color", new THREE.BufferAttribute(colors, 3));
    geometry.setAttribute("vSize", new THREE.BufferAttribute(sizes, 1));
    geometry.setAttribute(
      "randomDirection",
      new THREE.BufferAttribute(randomDirections, 3)
    );
    geometry.setAttribute(
      "randomSpeed",
      new THREE.BufferAttribute(randomSpeeds, 1)
    );

    // 纹理贴图
    const textureLoader = new THREE.TextureLoader();
    const particlesTexture = textureLoader.load("./texture/particles/1.png");

    // 创建点的材质
    const material = new THREE.PointsMaterial({
      size: 0.05, // 点的大小
      vertexColors: true, // 顶点颜色
      transparent: true, // 开启透明度
      opacity: 0.8, // 设置透明度
      depthWrite: false, // 设置深度写入
      blending: THREE.AdditiveBlending, // 设置混合模式(叠加)
      sizeAttenuation: true, // 设置距离衰减
      map: particlesTexture, // 纹理贴图
      alphaMap: particlesTexture, // 透明度贴图
      onBeforeCompile: (shader) => {
        shader.vertexShader = `
          attribute float vSize;
          ${shader.vertexShader}
        `.replace(`gl_PointSize = size;`, `gl_PointSize = size * vSize;`);
        shader.fragmentShader = `
          ${shader.fragmentShader}
        `;
      },
    });

    this.geometry = geometry;
    this.material = material;
  }
}

export default RandomSpacePoints;
